package com.sanjiahantang.youjiarole.module.me.favorite.presenters;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListAdapter;

import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseBean;
import com.sanjiahantang.youjiarole.base.BaseMvpActivity;
import com.sanjiahantang.youjiarole.base.BaseMvpPresenter;
import com.sanjiahantang.youjiarole.base.BaseView;
import com.sanjiahantang.youjiarole.base.CallBack;
import com.sanjiahantang.youjiarole.config.ConfigValue;
import com.sanjiahantang.youjiarole.config.SPConfig;
import com.sanjiahantang.youjiarole.module.cases.beans.CaseBean;
import com.sanjiahantang.youjiarole.module.cases.models.ICaseModel;
import com.sanjiahantang.youjiarole.module.cases.models.impl.CaseModel;
import com.sanjiahantang.youjiarole.module.cases.views.impl.CaseDetailActivity;
import com.sanjiahantang.youjiarole.module.me.favorite.views.FavoriteCaseView;
import com.sanjiahantang.youjiarole.utils.FileUtils;
import com.sanjiahantang.youjiarole.utils.SPUtils;
import com.squareup.picasso.Picasso;

import java.util.HashMap;
import java.util.Map;


/**
 * Created by zy on 2017/9/5.
 */

public class FavoriteCasePresenter extends BaseMvpPresenter<FavoriteCaseView> implements CallBack<CaseBean> {

    private QuickAdapter mAdapter;
    private ICaseModel caseModel;
    public FavoriteCasePresenter(){
        caseModel = new CaseModel();
    }

    public ListAdapter getAdapter(BaseMvpActivity context) {
        return mAdapter = new QuickAdapter<CaseBean.Item>(context, R.layout.item_case_list) {
            @Override
            protected void convert(BaseAdapterHelper helper, final CaseBean.Item item) {
                ImageView iv_case_collect = helper.getView(R.id.iv_case_collect);

                helper.setText(R.id.txtCaseTitle,item.getResidentialName());
                Picasso.with(context).load(FileUtils.rePath(item.getUrl())).placeholder(R.drawable.i_match).into((ImageView) helper.getView(R.id.ivCaseMain));
                helper.setText(R.id.tv_case_detail,item.getDoor() + "," + item.getStyle());
                helper.setText(R.id.tv_case_collectnum,String.valueOf(item.getCollectNumber()));
                iv_case_collect.setImageResource(R.drawable.i_heart_on);
                iv_case_collect.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Utils.showDialog((Activity) context, "提示", "确定删除吗？", "", "", new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Utils.dismissDialog();
                                switch (view.getId()) {
                                    case com.sanjiahantang.uilibrary.R.id.txtDialogCancel:
                                        break;
                                    case com.sanjiahantang.uilibrary.R.id.txtDialogSure:
                                        deleteCollectInfo(context,item.getId(), new BaseView.OnLoadListener() {
                                            @Override
                                            public void onLoad(Object content) {
                                                Utils.dismissDialog();
                                                if (content instanceof BaseBean){
                                                    BaseBean baseBean = (BaseBean) content;

                                                    if (baseBean.getCode() == RESPONSE_CODE_SUC){
                                                        Utils.showToast(context,baseBean.getMessage());
                                                        loadData(context);
                                                    }else {
                                                        Utils.showToast(context,"取消收藏失败");
                                                    }
                                                }
                                            }
                                        });
                                        break;
                                }
                            }
                        });


                    }
                });
                helper.getView().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Intent intent = new Intent(context,CaseDetailActivity.class);
                        intent.putExtra(KEY_ID,item.getId());
                        context.startActivity(intent);
                    }
                });


            }
        };
    }

    public void deleteCollectInfo(final Context context, int id, final BaseView.OnLoadListener listener) {
        Map<String,Object> param = new HashMap<>();
        param.put(KEY_TOKEN,ConfigValue.DATA_KEY);
        param.put(FavoriteCaseView.KEY_ROLE_TYPE,1);
        param.put(KEY_PHONE,SPUtils.get(context, SPConfig.PHONE,"").toString());
        param.put(KEY_TYPE,1);
        param.put(FavoriteCaseView.KEY_FAVORITE_ID,id);
        mView.showProgress();
        caseModel.deleteCollectInfo(context,param, new CallBack<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response, Context context, Object tag) {
                hideProgress();
                listener.onLoad(response);
            }

            @Override
            public void onFailure(String message) {
                hideProgress();
                Utils.showToast(context,message);
            }
        });
    }

    private void hideProgress() {
        if (mView != null) mView.hideProgress();
    }


    public void loadData(Context context) {
        mView.showProgress();
        Map<String,Object> param = new HashMap<>();
        param.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        param.put(KEY_PAGE,0);
        param.put(KEY_PAGE_SIZE,DEFAULT_PAGE_SIZE);
        Map<String,Object> map = new HashMap<>();
        map.put(KEY_PHONE, SPUtils.get(context, SPConfig.PHONE,"").toString());
        map.put(FavoriteCaseView.KEY_ROLE_TYPE,1);
        param.put(KEY_PARAM,map);
        caseModel.loadFavoriteCase(context,param,this);
    }

    @Override
    public void onSuccess(CaseBean response, Context context, Object tag) {
        mView.hideProgress();
        if (response.getCode() == RESPONSE_CODE_SUC){
            CaseBean.Data data = response.getData();
            mAdapter.clear();
            mAdapter.addAll(data.getList());
        }

    }

    @Override
    public void onFailure(String message) {
        mView.hideProgress();
    }
}
